package com.gmlive.common.ui.xui.helper

import android.view.View
import com.gmlive.common.ui.R
import com.gmlive.common.ui.xui.IAlphaViewHelper
import com.gmlive.common.ui.xui.ThemeUtils
import java.lang.ref.WeakReference

/**
 * 透明度辅助工具
 *
 * @author xuexiang
 * @since 2018/11/30 下午1:21
 */
class XUIAlphaViewHelper : IAlphaViewHelper {
    private var mTarget: WeakReference<View>

    /**
     * 设置是否要在 press 时改变透明度
     */
    private var mChangeAlphaWhenPress = false

    /**
     * 设置是否要在 disabled 时改变透明度
     */
    private var mChangeAlphaWhenDisable = false
    private val mNormalAlpha = 1f
    private var mPressedAlpha: Float
    private var mDisabledAlpha: Float

    constructor(target: View) {
        mTarget = WeakReference(target)
        mChangeAlphaWhenPress = ThemeUtils.resolveBoolean(target.context, R.attr.xui_switch_alpha_pressed, true)
        mChangeAlphaWhenDisable = ThemeUtils.resolveBoolean(target.context, R.attr.xui_switch_alpha_disabled, true)
        mPressedAlpha = ThemeUtils.resolveFloat(target.context, R.attr.xui_alpha_pressed, 0.5f)
        mDisabledAlpha = ThemeUtils.resolveFloat(target.context, R.attr.xui_alpha_disabled, 0.5f)
    }

    constructor(target: View, pressedAlpha: Float, disabledAlpha: Float) {
        mTarget = WeakReference(target)
        mPressedAlpha = pressedAlpha
        mDisabledAlpha = disabledAlpha
    }

    /**
     * @param current the view to be handled, maybe not equal to target view
     * @param pressed
     */
    override fun onPressedChanged(current: View, pressed: Boolean) {
        val target = mTarget.get() ?: return
        if (current.isEnabled) {
            target.alpha = if (mChangeAlphaWhenPress && pressed && current.isClickable) mPressedAlpha else mNormalAlpha
        } else {
            if (mChangeAlphaWhenDisable) {
                target.alpha = mDisabledAlpha
            }
        }
    }

    /**
     * @param current the view to be handled, maybe not  equal to target view
     * @param enabled
     */
    override fun onEnabledChanged(current: View, enabled: Boolean) {
        val target = mTarget.get() ?: return
        val alphaForIsEnable: Float = if (mChangeAlphaWhenDisable) {
            if (enabled) mNormalAlpha else mDisabledAlpha
        } else {
            mNormalAlpha
        }
        if (current !== target && target.isEnabled != enabled) {
            target.isEnabled = enabled
        }
        target.alpha = alphaForIsEnable
    }

    /**
     * 设置是否要在 press 时改变透明度
     *
     * @param changeAlphaWhenPress 是否要在 press 时改变透明度
     */
    override fun setChangeAlphaWhenPress(changeAlphaWhenPress: Boolean) {
        mChangeAlphaWhenPress = changeAlphaWhenPress
    }

    /**
     * 设置是否要在 disabled 时改变透明度
     *
     * @param changeAlphaWhenDisable 是否要在 disabled 时改变透明度
     */
    override fun setChangeAlphaWhenDisable(changeAlphaWhenDisable: Boolean) {
        mChangeAlphaWhenDisable = changeAlphaWhenDisable
        val target = mTarget.get()
        if (target != null) {
            onEnabledChanged(target, target.isEnabled)
        }
    }
}